<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="hAdd(0,1)">添加权限</el-button>
        </div>
        <el-table row-key="id" :data="list" border>
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template #default="{row}">
              <el-button v-if="row.type===1" type="text" @click="hAdd(row.id,2)">添加</el-button>
              <el-button type="text" @click="isEdit(row)">编辑</el-button>
              <el-button type="text" @click="hDel(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
      <!-- 新增权限的弹层 -->
      <el-dialog :visible.sync="showDialog" title="弹层标题" @close="resetForm">
        <!-- 表单内容 -->
        <el-form ref="formref" label-width="100px">
          <el-form-item label="权限名称">
            <el-input v-model="formData.name" />
          </el-form-item>
          <el-form-item label="权限标识">
            <el-input v-model="formData.code" />
          </el-form-item>
          <el-form-item label="权限描述">
            <el-input v-model="formData.description" />
          </el-form-item>
          <el-form-item label="权限启用">
            <el-switch
              v-model="formData.enVisible"
              active-text="启用"
              active-value="1"
              inactive-text="不启用"
              inactive-value="0"
            />
          </el-form-item>
        </el-form>

        <template #footer>
          <div style="text-align: right;">
            <el-button @click="showDialog = false">取消</el-button>
            <el-button type="primary" @click="hSubmit">确定</el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { getPermissionList, addPermission, delPermission, updatePermission } from '@/api/permissions'
import { render } from '@/utils/index'
export default {
  data() {
    return {
      edit: false, // 控制按钮触发的功能
      showDialog: false, // 控制弹层显示或影藏
      list: [], // 权限列表
      formData: {
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        enVisible: '0', // 开启
        type: 1, // 判断是页面级别,还是按钮级别
        pid: ''// 找到它的父级的id
      }
    }
  },
  created() {
    // 调用获取权限列表
    this.loadPermissionList()
  },
  methods: {
    // 编辑状态
    isEdit(row) {
      this.edit = false
      this.formData = { ...row }
      this.showDialog = true
    },
    // 删除权限
    hDel(id) {
      this.$confirm('您确定要删除权限吗').then(async() => {
        const res = await delPermission(id)
        console.log('删除权限', res)
        if (res.code !== 10000) return
        this.$message.success(res.message)
        // 更新函数
        this.loadPermissionList()
      }).catch(err => console.log(err))
    },
    // 添加权限函数
    hAdd(id, type) {
      this.edit = true
      this.formData.pid = id
      this.formData.type = type
      this.showDialog = true
    },
    // 表单重置
    resetForm() {
      this.$refs.formref.resetFields()
      this.formData = {
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        enVisible: '0', // 开启
        type: 1, // 判断是页面级别,还是按钮级别
        pid: ''// 找到它的父级的id
      }
    },
    // 点击确定按钮
    hSubmit() {
      // 兜底校验
      // 调用新增权限函数
      this.edit ? this.savePermission() : this.loadupdatePermission()
    },
    // 新增权限
    async savePermission() {
      try {
        const res = await addPermission(this.formData)
        console.log('addPermission', res)
        if (res.code !== 10000) return
        this.$message.success(res.message)
        this.loadPermissionList()
        this.showDialog = false
        // 保存数据
      } catch (err) {
        console.log('addPermission', err)
      }
    },
    // 更新权限
    async loadupdatePermission(data) {
      try {
        const res = await updatePermission(this.formData)
        console.log('updatePermission', res)
        if (res.code !== 10000) return
        this.$message.success(res.message)
        this.showDialog = false
        this.loadPermissionList()
        // 保存数据
      } catch (err) {
        console.log('updatePermission', err)
      }
    },
    // 获取权限列表函数
    async loadPermissionList() {
      try {
        const res = await getPermissionList()
        console.log('getPermissionList', res)
        this.list = render(res.data)
        console.log(this.list)
        // 保存数据
      } catch (err) {
        console.log('getPermissionList', err)
      }
    }
  }
}
</script>
